package org.example;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Arrays;

/**
 * 复制图片到新文件中,只要有字节数组,就可以写到文件中
 *
 */
public class AppIoStream {
    public static void main(String[] args) throws Exception {

        String idea = "idea.jpg";
        InputStream inputStream = AppIoStream.class.getClassLoader().getResourceAsStream(idea);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];
        int length = 0;
        while ((length = inputStream.read(buffer)) != -1) {
            //可以看到当自定义数组中读满的时候,返回走一次长度
            System.out.println("进来了,length=" + length);
            byteArrayOutputStream.write(buffer, 0, length);
        }

        inputStream.close();
        byte[] resultBytes = byteArrayOutputStream.toByteArray();
        System.out.println(resultBytes.length);

        String s = Arrays.toString(resultBytes);
        System.out.println(s);

        byte[] ideaBytes = {-1, -40, -1, -32, 0, 16, 74, 70, 73, 70, 0, 1, 1, 1, 0, -112, 0, -112, 0, 0, -1, -37, 0, 67, 0, 3, 2, 2, 3, 2, 2, 3, 3, 3, 3, 4, 3, 3, 4, 5, 8, 5, 5, 4, 4, 5, 10, 7, 7, 6, 8, 12, 10, 12, 12, 11, 10, 11, 11, 13, 14, 18, 16, 13, 14, 17, 14, 11, 11, 16, 22, 16, 17, 19, 20, 21, 21, 21, 12, 15, 23, 24, 22, 20, 24, 18, 20, 21, 20, -1, -37, 0, 67, 1, 3, 4, 4, 5, 4, 5, 9, 5, 5, 9, 20, 13, 11, 13, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, -1, -64, 0, 17, 8, 0, 34, 0, 38, 3, 1, 17, 0, 2, 17, 1, 3, 17, 1, -1, -60, 0, 27, 0, 0, 2, 2, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 3, 4, 5, 7, 8, 2, -1, -60, 0, 44, 16, 0, 1, 3, 3, 4, 1, 1, 7, 5, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 0, 5, 17, 6, 7, 18, 33, 49, 65, 8, 19, 20, 34, 35, 81, -127, 21, 97, 113, -111, -95, -1, -60, 0, 27, 1, 0, 2, 3, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 3, 6, 7, 4, 1, 2, -1, -60, 0, 44, 17, 0, 1, 3, 3, 2, 6, 1, 2, 7, 0, 0, 0, 0, 0, 0, 0, 1, 0, 2, 3, 4, 5, 17, 33, 49, 6, 18, 65, 81, 97, 113, -79, -95, -16, 34, 38, 66, 98, -111, -78, -15, -1, -38, 0, 12, 3, 1, 0, 2, 17, 3, 17, 0, 63, 0, -28, -6, -107, 68, -104, 45, -38, 30, -25, 116, -73, -75, 49, -128, -41, -69, 115, 60, 82, -91, -31, 88, 7, 25, -15, 92, -81, -87, -115, -114, -27, 43, 71, -74, 112, 13, -26, -19, 66, -53, -123, 48, 111, 43, -13, -128, 78, 14, -121, 25, -37, 26, -29, 77, 85, 103, 52, -91, -55, -101, -101, 80, 28, -113, -63, -25, 70, 82, 115, -108, -29, -44, -28, 122, 10, 107, 110, -90, -110, -21, 59, 105, -23, 70, 92, 126, -98, 79, -127, -9, -86, -88, 95, -83, 21, -68, 52, -2, 75, -100, 124, -102, 100, 108, 65, 30, 8, -48, -6, -35, 81, -72, 91, -92, 90, -27, 42, 60, -106, -53, 110, 39, -17, -32, -113, -72, 62, -94, -66, -21, 104, -89, -73, -52, 96, -88, 110, 28, 62, -66, 71, 112, -112, -59, 43, 38, 96, -110, 51, -112, 85, 106, -31, 83, 47, 108, -96, 58, -14, 16, 84, 16, 20, -96, 10, -107, -32, 126, -26, -67, 82, -60, -63, 36, -115, 97, 56, 4, -127, -109, -80, -49, 83, -23, 116, 46, -42, -40, -76, -27, -1, 0, 112, 6, -97, -44, 47, -34, 99, -38, 25, -45, 114, -82, -111, -1, 0, 69, -112, -37, 14, 44, -57, 105, 111, 100, -107, -95, 64, -91, 77, -74, -79, -48, -14, 82, 115, -116, -25, -124, 71, 19, -119, 32, -121, 107, -17, 94, -95, 110, 92, 97, 124, -85, -75, 26, 58, 107, 69, 65, 100, 66, 49, -114, 83, -95, 3, 64, 116, -33, 78, -87, 50, -44, 98, 92, 118, 87, 82, -21, 55, -89, -50, 76, -5, 93, -30, 5, -71, -106, -61, -40, 105, 72, 125, -89, -42, -94, -96, 70, 74, -66, -118, 113, -126, 6, 9, -56, 61, 97, -123, 37, 68, -44, 15, 50, 82, -72, -80, -99, 50, 52, -47, 100, -73, 43, -67, 125, -30, 49, 13, -62, 99, 43, 65, -56, 14, -41, 7, -72, -20, -107, -41, 61, -67, 65, -94, 102, 76, 115, -21, 56, -61, -31, 45, -72, -93, -107, 39, -76, 103, -65, -55, -83, 21, -46, 58, -31, -61, 114, 84, 85, 30, 119, -79, -38, 56, -18, 50, 90, 55, -12, 85, 116, 83, 50, 18, 30, -51, 51, -47, 41, 86, 110, -91, 83, -64, 121, -104, -13, -93, -69, 33, -113, -118, 97, -73, 18, -89, 24, -27, -57, -34, 36, 28, -108, -25, -45, 35, -84, -44, 83, 49, -17, -115, -52, -115, -36, -82, 32, -128, 119, -63, -24, 113, -41, 10, 88, 92, -58, 72, -41, -56, -34, 102, -126, 50, 54, -56, -22, 51, -45, 41, -81, 114, -3, -96, 53, 29, -101, 119, 108, -9, -35, -68, -42, 16, -76, 28, -121, -76, -38, -94, 61, 42, -22, -46, 74, 10, 12, -116, -83, -128, 11, 78, 12, -98, 41, 57, 3, -62, 79, 126, 65, -85, 112, -19, -98, 107, 53, 43, -29, -88, 57, 115, -100, 78, 123, -116, 1, -97, -107, 113, -30, 91, -108, 87, 89, 97, -88, -124, 97, -68, -72, -63, -23, -126, 86, -120, -36, -19, -22, -36, 25, -70, 118, -55, -94, -82, -69, -128, -58, -86, -45, -106, -74, -101, 114, 27, 22, -9, 10, -93, -78, 82, -107, 54, -124, -88, -106, -48, -91, 41, 8, 24, 28, -71, 113, 74, -128, 4, 100, -118, -76, -86, 114, -40, 59, 59, 49, -23, -5, 39, 114, 121, -27, 114, 95, -57, -111, -97, -53, 85, -91, -46, 15, -54, 117, 7, -9, -113, -20, -60, -114, 121, -15, 95, 28, 61, -63, -8, 42, 74, -50, 83, 116, 80, -123, -112, -107, 115, -115, 38, 12, 104, -53, -75, 65, 120, -78, -114, 33, -23, 44, 37, -27, -116, -100, -100, 114, 29, 12, -109, -43, 59, 117, -49, 16, 50, 8, -30, 104, -27, 27, -111, -109, -25, 125, 6, -66, -45, -26, -35, -28, -114, -99, -112, 71, 27, 127, 8, -36, -128, 79, 115, -66, -101, -8, 86, 109, 115, -84, 44, 71, 82, 39, 105, -85, 108, -121, 0, -7, 86, -120, 109, 39, -105, -13, -14, -1, 0, -93, -6, -85, 13, -73, -120, 105, 41, -31, 49, -43, -46, -75, -18, 27, 16, -42, -116, -5, -45, -22, 63, -123, 84, -83, -114, -94, -87, -36, -51, -109, 25, -5, -45, 31, 10, 57, -73, -11, -67, 13, 80, 98, 69, -115, 109, -73, -87, 92, -116, 104, -83, 4, 36, -100, -25, 39, 3, -49, 67, -19, 75, 46, 28, 65, 81, 93, 17, -90, 99, 91, 28, 71, -12, -76, 13, 117, -50, -89, -33, 108, 46, 90, 123, 108, 80, -56, 39, 121, 46, 120, -22, 79, -64, -1, 0, 86, 42, -86, -55, -70, 40, 66, 40, 66, 40, 66, 40, 66, 40, 66, -1, -39};
        String newIdea = "ideaByteArray.jpg";


        File destFile = new File(System.getProperty("user.dir") +
                "/io/src/main/resources/" + newIdea);

        if (destFile.exists()) {
            destFile.delete();
        }


        FileOutputStream fos = new FileOutputStream(System.getProperty("user.dir") + "/io/src/main/resources/" + newIdea);

        fos.write(ideaBytes);
        fos.close();


    }
}
